﻿/*
 * Copyright (C) 2023 - 2024 lchfj.cn
 * 
 * This file is part of FastestHost.
 * 
 * FastestHost is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * FastestHost is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with FastestHost. If not, see <https://www.gnu.org/licenses/>.
 */

using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Cn.Lchfj.FastestHost;

internal static partial class ConsoleHelper
{
    /// <summary>
    /// 显示控制台
    /// </summary>
    [LibraryImport("kernel32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static partial bool AllocConsole();

    [LibraryImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static partial bool ShowWindow(IntPtr hWnd, int nCmdShow);

    private static bool ConsoleHaveAlloc = false;
    private static bool ConsoleHaveShow = false;

    internal static void ShowWindow()
    {
        if (!ConsoleHaveAlloc)
        {
            AllocConsole();
            ConsoleHaveAlloc = true;
            ConsoleHaveShow = true;
            return;
        }


        var currentProcess = Process.GetCurrentProcess();
        var hWnd = currentProcess.MainWindowHandle;
        ShowWindow(hWnd, 1);
        ConsoleHaveShow = true;
    }
    internal static void HiddenWindow()
    {
        if (!ConsoleHaveAlloc)
            return;


        var currentProcess = Process.GetCurrentProcess();
        var hWnd = currentProcess.MainWindowHandle;
        ShowWindow(hWnd, 0);
        ConsoleHaveShow = false;
    }




    internal static void SetForegroundColor(ConsoleColor value)
    {
        if (!ConsoleHaveShow)
            return;

        Console.ForegroundColor = value;
    }

    internal static string ReadLine()
    {
        return Console.ReadLine() ?? string.Empty;
    }

    internal static void Write(object obj)
    {
        if (!ConsoleHaveShow)
            return;

        Console.Write(obj);
    }

    internal static void WriteLine()
    {
        if (!ConsoleHaveShow)
            return;

        Console.WriteLine();
    }

    internal static void WriteLine(string message)
    {
        if (!ConsoleHaveShow)
            return;

        Console.WriteLine(message);
    }

    public static void ShowException(Exception ex)
    {
        ShowWindow();
        Thread.Sleep(100);
        WriteLine();

        SetForegroundColor(ConsoleColor.Red);
        WriteLine("发生错误！！！");

        SetForegroundColor(ConsoleColor.White);
        WriteLine(ex.Message);
        WriteLine(ex.ToString());

        WriteLine();
        WriteLine("【Enter】键继续");
        ReadLine();

        HiddenWindow();
    }
}
